#include<pthread.h>

// 对pthread_mutex_t，进行封装，可以进行提供一些加锁解锁的接口。
class Mutex
{
public:
    Mutex(pthread_mutex_t* mutex = nullptr)
    :_mutex(mutex)
    {}

    ~Mutex(){}

    void lock()
    {
        if(_mutex)
        pthread_mutex_lock(_mutex);
    }

    void unlock()
    {
        if(_mutex)
            pthread_mutex_unlock(_mutex);
    }

private:
    pthread_mutex_t* _mutex;
};

// 二次封装，直接通过构造函数和析构函数来加锁和解锁
class LockGuard
{
public:
    LockGuard(pthread_mutex_t* mutex = nullptr)
    :_mutex(mutex)
    {
        // 构造函数中加锁
        _mutex.lock();
    }

    ~LockGuard()
    {
        // 析构函数中解锁
        _mutex.unlock();
    }
private:
    Mutex _mutex;
};